package com.csh.wx.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Component
@Slf4j
public class SubscribeMsgHandler implements WxChatMsgHandler {

    @Override
    public WxChatMsgTypeEnum getMsgType() {
        return WxChatMsgTypeEnum.SUBSCRIBE;
    }

    @Override
    public String dealMsg(Map<String, String> messageMap) {
        log.info("触发用户关注事件！");
        String fromUserName = messageMap.get("FromUserName");
        String toUserName = messageMap.get("ToUserName");
        String subscribeContent = "感谢您的关注!欢迎来Code社区";

//        // 获取当前时间并格式化为字符串
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//        String createTime = sdf.format(new Date());

        // 将当前时间转换为Unix时间戳,时间戳以秒为单位
        long unixTimeStamp = new Date().getTime() / 1000;
        // 将Unix时间戳转换为字符串
        String timeStampString = String.valueOf(unixTimeStamp);

        String content = "<xml>\n" +
                "  <ToUserName><![CDATA[" + fromUserName + "]]></ToUserName>\n" +
                "  <FromUserName><![CDATA[" + toUserName + "]]></FromUserName>\n" +
                "  <CreateTime>"+ timeStampString +"</CreateTime>\n" +
                "  <MsgType><![CDATA[text]]></MsgType>\n" +
                "  <Content><![CDATA[" + subscribeContent + "]]></Content>\n" +
                "</xml>";
        return content;
    }

}
